<?php
/**
 * CouponModel.class.php
 * 优惠类模型
 *
 * @author 王中艺 <[email address]>
 * @date 2017-02-19
 */

namespace Admin\Model;
use Think\Model;

class CouponModel extends Model{
	protected 	$trueTableName 	= 't_coupon_code';
	public 	$errMsg 	= false;

	/**
	 * 获取优惠码列表
	 * @param   $page 页码数
	 * @return  $list
	 */
	function getCodeList($page, &$pageHtml){
		$list 	= $this->page($page, C('PAGE_COUNT'))
			->order('id desc')
			->where('status != -1')
			//->cache(true)
			->select();
		foreach($list as &$item){
			$item['create_date']	= date('Y-m-d', $item['create_time']);
			$item['end_date']	= date('Y-m-d', $item['end_time']);
			$item['status_text']	= C('CODE_STATUS.'.$item['status']);
		}

		$count 	= $this->cache(true)->where('status != -1')->count();
		$Page 	= new \Think\Page($count, C('PAGE_COUNT'));
		$pageHtml 	= $Page->angular_show();

		return $list;
	}

	/**
	 * 添加优惠码
	 * @param   $code 优惠码
	 * @param   $amount 抵用金额
	 * @param   $limitAmount 抵扣金额
	 * @param   $endTime 截止日期
	 * @return  $codeId
	 */
	function addCode($code, $amount, $limitAmount, $endTime){
		$codeInfo 	= array(
			'code'	=> $code,
			'amount'	=> $amount,
			'limit_amount'	=> $limitAmount,
			'create_time'	=> time(),
			'end_time'	=> $endTime
		);

		$codeId 	= $this->add($codeInfo);
		if($codeId === false){
			$this->errMsg 	= '优惠码添加失败，请重试!';
			return false;
		}

		return $codeId;
	}

	/**
	 * 更改优惠码状态
	 * @param   $codeId 优惠码ID
	 * @param   $status 状态
	 * @return  boolean [description]
	 */
	function changeCodeStatus($codeId, $status){
		$data 	= array(
			'id'	=> $codeId,
			'status'	=> $status
		);

		$result 	= $this->save($data);
		if($reslut === false){
			$this->errMsg 	= '优惠码状态变更失败,请重试!';
			return false;
		}

		return true;
	}

	/**
	 * 获取使用记录
	 * @param 	$timeS 	开始时间
	 * @param 	$timeE 	结束时间
	 * @param 	$page 	页码数
	 * @param 	&$pageHtml 	页面HTML
	 * @param 	&$total 	合计信息
	 * @return 	$list
	 */
	function getUseLog($timeS, $timeE, $page, &$pageHtml, &$total){
		$where 	= '1 = 1';
		if($timeS)
			$where 	.= ' and a.use_time >= '.$timeS;
		if($timeE)
			$where 	.= ' and a.use_time <= '.$timeE;

		$list 	= M('t_coupon_code_use a', null)
			->join('left join t_coupon_code b on a.code_id = b.id')
			->join('left join t_user c on a.user_id = c.id')
			->where($where)
			->cache(true)
			->page($page, C('PAGE_COUNT'))
			->field('a.id, a.user_id, a.code_id, a.use_time, b.code, b.amount, c.nickname, c.truename')
			->select();

		foreach($list as &$item){
			$item['use_date']	= date('Y-m-d', $item['use_time']);
		}

		if($total === true){
			$total 	= M('t_coupon_code_use a', null)
				->join('left join t_coupon_code b on a.code_id = b.id')
				->where($where)
				->cache(true)
				->field('count(a.id) count, sum(b.amount) amount')
				->find();
		}
			

		$count 	= M('t_coupon_code_use a')->where($where)->cache(true)->count();
		$Page 	= new \Think\Page($count, C('PAGE_COUNT'));
		$pageHtml 	= $Page->angular_show();

		return $list;
	}

	/**
	 * @param 	$start 	开始位置
	 * @param 	$lenght 	到处长度
	 * @param 	$timeS 	开始时间
	 * @param 	$timeE 	结束时间
	 * @return 	list or count
	 */
	function outExcelUseLog($start, $length, $timeS, $timeE){
		$where 	= '1 = 1';
		if($timeS)
			$where 	.= 'a.use_time >= '.$timeS;
		if($timeE)
			$where 	.= 'a.use_time <= '.$timeE;

		if($start === false){
			$count 	= M('t_coupon_code_use a', null)
				->where($where)
				->cache(true)
				->count();
			return $count;
		}else{
			$list 	= M('t_coupon_code_use a', null)
				->join('left join t_coupon_code b on a.code_id = b.id')
				->join('left join t_user c on a.user_id = c.id')
				->where($where)
				->cache(true)
				->limit($start, $length)
				->field('a.id, a.user_id, c.nickname, c.truename, b.code, b.amount, a.use_time')
				->select();
			return $list;
		}
	}
}